#!/bin/sh
#
# aprx daemon control script.
# Written for Slackware Linux by JK Wood <joshuakwood@gmail.com>

BIN=/sbin/aprx
CONF=/etc/aprx.conf
PID=/var/run/aprx.pid

aprx_start() {
  # Sanity checks.
  if [ ! -r $CONF ]; then # no config file, exit:
    echo "$CONF does not appear to exist. Abort."
    exit 1
  fi

  if [ -s $PID ]; then
    echo "aprx appears to already be running?"
    exit 1
  fi

  echo "Starting aprx daemon..."
  if [ -x $BIN ]; then
    $BIN -f $CONF
  fi
}

aprx_stop() {
  echo "Shutdown aprx gracefully..."
  if [ -r $PID ]; then
    kill -HUP $(cat $PID)
    rm -f $PID
  else
    killall -HUP -q aprx
  fi
  echo
}

aprx_restart() {
  aprx_stop
  sleep 3
  aprx_start
}

aprx_status() {
  if [ -e $PID ]; then
    echo "aprx is running."
  else
    echo "arpx is stopped."
    exit 1
  fi
}

case "$1" in
  start)
    aprx_start
    ;;
  stop)
    aprx_stop
    ;;
  restart)
    aprx_restart
    ;;
  status)
    aprx_status
    ;;
  *)
  echo "usage: `basename $0` {start|stop|restart|status}"
esac
